package com.shiro.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.shiro.service.UserService;
import com.shiro.service.impl.UserServiceImpl;

/**
 * 登录控制
 */
@Controller
@RequestMapping("/user")
public class UserController{
	
	UserService userService = new UserServiceImpl();
	
	/**
	 * 登录操作
	 * */
	@RequestMapping(value = "/login")
	protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		
		String message = userService.login(username, password);
		String status = message.split(",")[0]; // 获取登录状态
		String mes = message.split(",")[1];	// 获取登录信息
		
		req.setAttribute("error", mes);
		if (status.equals("false")) {// 出错了，返回登录页面
			req.getRequestDispatcher("/user/loginPage").forward(req, resp);
		} else {// 登录成功
			req.getRequestDispatcher("/WEB-INF/jsp/loginSuccess.jsp").forward(req, resp);
		}
	}
	
	/**
	 * 登陆页面
	 * */
	@RequestMapping(value = "/loginPage")
	protected String loginPage(){
		return "/WEB-INF/jsp/login.jsp";
	}
}